package com.hairline.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.hairline.bean.Comment;
import com.hairline.bean.CommentExample;
import com.hairline.bean.User;

import com.hairline.mapper.CommentMapper;
import com.hairline.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
@Service
public class WxCommentServicesImpl implements WxCommentServices {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public Map<String, Object> getCommentList(Integer valueId, Integer type, Integer showType, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andValueIdEqualTo(valueId).andTypeEqualTo(type.byteValue());
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        //用一个List接收最外层数据
        HashMap<String, Object> boundaryMap = new HashMap<>();
        //用arraylist接收第一层数据
        ArrayList<Object> data = new ArrayList<>();
         //用mapper接收第二层对象
        HashMap<String, Object> secondMap = new HashMap<>();
        //获得单条评论的用户信息以及 用户评论
        for (Comment comment : comments) {
            Date addTime = comment.getAddTime();
            String content = comment.getContent();
            String picUrls = comment.getPicUrls();
            secondMap.put("addTime",addTime);
            secondMap.put("content",content);
            secondMap.put("picList",picUrls);
            //获取用户信息
            Integer userId = comment.getUserId();

            User user = userMapper.selectByPrimaryKey(userId);
            //用userMapper接收最内层对象
            HashMap<String, Object> userInformationMap = new HashMap<>();
            userInformationMap.put("nickName",user.getNickname());
            userInformationMap.put("avatarUrl",user.getAvatar());
            secondMap.put("userInfo",userInformationMap);
            //传给数组
            data.add(secondMap);
        }
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        long total = commentPageInfo.getTotal();

        boundaryMap.put("data",data);
        boundaryMap.put("count",total);
        boundaryMap.put("currentPage",page);

        PageHelper.startPage(page,size);

        return boundaryMap;
    }

    @Override
    public Map getCount(Integer valueId, Integer type) {
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andValueIdEqualTo(valueId).andTypeEqualTo(type.byteValue());
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        int allCount = comments.size();
        int hasPicCount=0;
        for (Comment comment : comments) {
            if(!StringUtil.isEmpty(comment.getPicUrls())&&!"[]".equals(comment.getPicUrls()))
            {
                    hasPicCount++;
            }
        }

        HashMap<String, Integer> voMap = new HashMap<>();
        voMap.put("allCount",allCount);
        voMap.put("hasPicCount",hasPicCount);


        return voMap;
    }
}
